<?php
// +----------------------------------------------------------------------
// | CSMITSM  [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2025 https://www.fastadmin.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: chenshiming
// +----------------------------------------------------------------------

namespace app\admin\controller\csmitsm;

use addons\csmitsm\library\sdnode\xstack\utils\XsDatetimeUtils;
use Exception;
use addons\csmitsm\library\xcore\xcore\utils\XcRequestUtils;
use addons\csmitsm\library\xcore\xcore\base\XcASubmitBackend;
use addons\csmitsm\library\xcore\xcore\utils\XcAdminTabUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcCrontaskUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcReflectionClassUtils;

/**
 * 定时器配置
 *
 * @icon fa fa-circle-o
 */
class Xcflowcrontask extends XcASubmitBackend
{

    /**
     * Xcflowcrontask模型对象
     * @var \app\admin\model\csmitsm\Xcflowcrontask
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\csmitsm\Xcflowcrontask;
        $this->assign("statusList", $this->model->getStatusList());
        $this->assign("lastrunIssuccessList", $this->model->getLastrunIssuccessList()); 

        // csmitsm:设置页面tabs
        $xc_tabid = XcRequestUtils::param(["ids", "id", "xc_tabid"]);
        $this->assignconfig("xc_tabid", $xc_tabid);
        XcAdminTabUtils::append_control_initialize('xcflowcrontask', $xc_tabid, __CLASS__, $this->view);

        if(true){
            $ROOT_PATH = ROOT_PATH;
            $command = "
菜单：计划任务-添加任务 ，添加每分钟执行任务 <BR>
执行周期：N分钟，1分钟 <BR>
脚本：php {$ROOT_PATH}/public/index.php /addons/csmitsm/xc_tools/cron";
            $this->view->assign("command_guide", $command);
        }
       
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
    //  */
    protected function beforeCreateOrUpdateSubmitSubmit(&$params)
    {
        $taskcls = $params['taskcls'];
        $taskmethod = $params['taskmethod'];
        $cronexpression = $params['cronexpression'];

        XcReflectionClassUtils::assertMethodExists($taskcls, $taskmethod);
        $nextimes = XcCrontaskUtils::nexttimes($cronexpression, 3);

        $params["nextruntime1"] = $nextimes[0];
        $params["nextruntime2"] = $nextimes[1];
        $params["nextruntime3"] = $nextimes[2];
    }

    public function testCrontask()
    {
        $taskcls = XcRequestUtils::get("taskcls");
        $taskmethod = XcRequestUtils::get("taskmethod");

        $taskResult = null;
        $result = null;
        try {
            XcReflectionClassUtils::assertMethodExists($taskcls,$taskmethod);
            $taskInstance = XcReflectionClassUtils::newInstance($taskcls);
            $result = $taskInstance->$taskmethod();
            $loggerMessage = $taskInstance->getLoggerMessage();
        } catch (Exception $e) {
            $result = false;
            $loggerMessage = "task任务类名或任务方法名不存在";
        }

        $this->success(null, null, [
            'result' => $result,
            'loggerMessage' => $loggerMessage
        ]);
    }


    public function testCronexpression()
    {
        $cronexpression = XcRequestUtils::get("cronexpression");

        $result = null;
        $loggerMessage = "下次运行时间<BR/>";
        try {
            $nextimes = XcCrontaskUtils::nexttimes($cronexpression, 6);
            foreach ($nextimes as $nextime) {
                $loggerMessage .= XsDatetimeUtils::timestampToString($nextime) . "<BR>";
            }
            $result = true;
        } catch (Exception $e) {
            $result = false;
            $loggerMessage = "表达式格式错误";
        }

        $this->success(null, null, [
            'result' => $result,
            'loggerMessage' => $loggerMessage
        ]);
    }
}
